<?php
session_start();
if(!@$_SESSION['username']) {
  echo "<script>window.location.href='login.php';</script>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>add ticket</title>
</head>
<body>
	<?php
		include_once('../../libraries/esaydb.class.php');
		if($_POST) {
			$map = $_POST;
			$db = new EasyDB();
			if($_POST['sid']) {
				$set = array(
					'station_name'=>$_POST['station-num'],
					'station_start'=>$_POST['user-from'],
					'station_end'=>$_POST['user-to'],
					'time_start'=>$_POST['time_start'],
					'time_end'=>$_POST['time_end'],
					'number'=>$_POST['have'],
					'num_none'=>$_POST['none'],
				);
				$where = array(
					'sid' => $_POST['sid']
				);
				$result  = $db->update('ob_station',$set,$where);

				$set = array(
					'station_name'=>$_POST['station-num'],
					'station_start'=>$_POST['user-from'],
					'station_end'=>$_POST['user-to'],
					'time_start'=>$_POST['time_start'],
					'time_end'=>$_POST['time_end'],
					'number'=>$_POST['have'],
					'num_none'=>$_POST['none'],
				);
				$where = array(
					'sid' => $_POST['sid']
				);
				$result  = $db->update('ob_station',$set,$where);

				if($map['have-p'] && $map['none-p']) {
					$haveset = array(
						'price'=>$_POST['have-p']
					);
					$havewhere = array(
						'sid' => $_POST['sid'],
						'site_type'=>'have'
					);
					$result  = $db->update('ob_ticket',$haveset,$havewhere);
					$noset = array(
						'price'=>$_POST['none-p']
					);
					$nowhere = array(
						'sid' => $_POST['sid'],
						'site_type'=>'none'
					);
					$result  = $db->update('ob_ticket',$noset,$nowhere);
					if($result) echo "<script>alert('修改成功');exit(1)</script>";
				}elseif(!$map['have-p'] && $map['none-p']){
						$noset = array(
						'price'=>$_POST['none-p']
					);
					$nowhere = array(
						'sid' => $_POST['sid'],
						'site_type'=>'none'
					);
					$result  = $db->update('ob_ticket',$noset,$nowhere);
					if($result) echo "<script>alert('修改成功');exit(1)</script>";
				}elseif($map['have-p'] && !$map['none-p']){
					$haveset = array(
						'price'=>$_POST['have-p']
					);
					$havewhere = array(
						'sid' => $_POST['sid'],
						'site_type'=>'have'
					);
					$result  = $db->update('ob_ticket',$haveset,$havewhere);
					if($result) echo "<script>alert('修改成功');exit(1)</script>";
				}
			}else{
				$map['station_name'] = $map['user-from'].'-'.$map['user-to'];
				$sqlOrderAdd = "INSERT INTO ob_station SET sid = ?, station_name = ?, station_start = ?, station_end = ?, time_start = ?, time_end = ?, number = ?, num_none = ?";
				$resultOrder = $db->querySql($sqlOrderAdd, array($map['station-num'], $map['station_name'], $map['user-from'], $map['user-to'], $map['time-from'], $map['time-to'], $map['have'], $map['none']));
				if($resultOrder) {
					if($map['none']&&$map['have']) {
						$ticketAddHave = "INSERT INTO ob_ticket SET sid = ?, site_type = ?, price = ?";
						$ticketAddNone = "INSERT INTO ob_ticket SET sid = ?, site_type = ?, price = ?";
						$have = $db->querySql($ticketAddHave,array($map['station-num'], 'have',$map['have-p']));
						$none = $db->querySql($ticketAddNone,array($map['station-num'], 'none',$map['none-p']));
					}elseif($map['none']&&!$map['have']){
						$ticketAddNone = "INSERT INTO ob_ticket SET sid = ?, site_type = ?, price = ?";
						$db->querySql($ticketAddNone,array($map['station-num'], 'none',$map['none-p']));
					}elseif($map['have']&&!$map['none']){
						$ticketAddHave = "INSERT INTO ob_ticket SET sid = ?, site_type = ?, price = ?";
						$db->querySql($ticketAddHave,array($map['station-num'], 'have',$map['have-p']));
					}
					echo "<script>alert('添加成功');exit(1)</script>";
				}else{
					echo "<script>alert('添加失败');history.go(-1);</script>";
				}
			}

		}else{
			echo "<script>alert('参数错误');history.go(-1);</script>";
		}
	?>
</body>
</html>